﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports Boogaloo.Base
Imports System.Xml
Imports System.Web

Namespace Rss

    Public MustInherit Class RssBase
        Inherits BizObject


        Protected Shared Function MakeItem(ByVal element As String, ByVal value As String) As String
            Dim dt As New DateTime
            If Not String.IsNullOrEmpty(value) And Not String.Equals(value, dt.ToString("r")) Then
                Return String.Format("<{0}>{1}</{0}>", element, GetSafeXml(value))
            Else
                Return String.Empty
            End If
        End Function

        Protected Shared Function GetSafeXml(ByVal value As String) As String
            Return HttpContext.Current.Server.HtmlEncode(value)
        End Function

        ''' <summary>
        ''' Quickly adds item to HttpContext.Current.Cache
        ''' </summary>
        ''' <remarks></remarks>
        Protected Shared Sub CacheData(ByVal key As String, ByVal data As Object, ByVal cacheDurationMinutes As Integer)
            BizObject.Cache.Insert(key, data, Nothing, DateTime.Now.AddMinutes(cacheDurationMinutes), TimeSpan.Zero)
        End Sub

        ''' <summary>
        ''' Retrieves the value of a child element InnerText and returns as Uri
        ''' </summary>
        Protected Shared Function GetChildItemInnerTextUri(ByVal item As XmlNode, ByVal child_local_name As String) As Uri
            Dim text As String = GetChildItemInnerText(item, child_local_name)
            If Not String.IsNullOrEmpty(text) Then
                Try
                    Return New Uri(text)
                Catch ex As Exception
                    Return Nothing
                End Try

            Else
                Return Nothing
            End If
        End Function

        ''' <summary>
        ''' Retrieves the value of a child element InnerText and returns as DateTime
        ''' </summary>
        Protected Shared Function GetChildItemInnerTextDateTime(ByVal item As XmlNode, ByVal child_local_name As String) As DateTime
            Dim text As String = GetChildItemInnerText(item, child_local_name)
            Dim dt As DateTime = Nothing
            If Not String.IsNullOrEmpty(text) Then
                Try
                    dt = DateTime.Parse(text)
                Catch ex As Exception
                    dt = Nothing
                End Try        
            End If
            Return dt
        End Function

        Protected Shared Function GetChildItemInnerTextInteger(ByVal item As XmlNode, ByVal child_local_name As String) As Integer
            Dim text As String = GetChildItemInnerText(item, child_local_name)
            Dim value As Integer = -1
            If Not String.IsNullOrEmpty(text) Then
                Try
                    value = CType(text, Integer)
                Catch ex As Exception
                    value = -1
                End Try
            End If
            Return value
        End Function

        ''' <summary>
        ''' Retrieves the value of a child element InnerText
        ''' </summary>
        Protected Shared Function GetChildItemInnerText(ByVal item As XmlNode, ByVal child_local_name As String) As String
            Dim text As String = String.Empty
            If item.SelectSingleNode(child_local_name) IsNot Nothing Then text = item.SelectSingleNode(child_local_name).InnerText
            Return text
        End Function

    End Class

End Namespace